package ca.etsmtl.log430.lab2;

import java.util.ArrayList;
import java.util.Observable;

import ca.etsmtl.log430.common.Menus;
import ca.etsmtl.log430.common.Project;
import ca.etsmtl.log430.common.Resource;

public class ListRoleAssignedToProject extends Communication{

	public ListRoleAssignedToProject(Integer registrationNumber,
			String componentName) {
		super(registrationNumber, componentName);
	}


	public void update(Observable thing, Object notificationNumber){
		Menus menu = new Menus();
		Project myProject = new Project();
		ArrayList<Integer> roleList = new ArrayList<Integer>();

		try {
			if (registrationNumber.compareTo((Integer)notificationNumber) == 0){

				addToReceiverList("ListProjectsComponent");

				//Signals the display of the projects and prompt the user to pick it
				signalReceivers("ListProjectsComponent");

				myProject = menu.pickProject(CommonData.theListOfProjects.getListOfProjects());

				myProject.getResourcesAssigned().goToFrontOfList();

				roleList = calculateRoles(myProject, "current");

				System.out.println("");
				System.out.println("Pendant l'execution courante, \n " +
						"Dans le projet : " + myProject.getID() + "\n\t"
						+ roleList.get(0) + " Analystes \n\t"
						+ roleList.get(1) + " Programmeurs \n\t"
						+ roleList.get(2) + " Concepteurs \n\t"
						+ roleList.get(3) + " Testeurs");

				CommonData.theListOfResources.getListOfResources().goToFrontOfList();
				roleList = calculateRoles(myProject, "previous");

				System.out.println("");
				System.out.println("Avant l'execution courante, \n " +
						"Dans le projet : " + myProject.getID() + "\n\t"
						+ roleList.get(0) + " Analystes \n\t"
						+ roleList.get(1) + " Programmeurs \n\t"
						+ roleList.get(2) + " Concepteurs \n\t"
						+ roleList.get(3) + " Testeurs");

				System.out.println("");
				System.out.println("");
				System.out.println("Sa date de debut et de fin sont: "+ myProject.getStartDate()+" --- "+ myProject.getEndDate());
			}
		}catch (Exception e){
			//Log error within DB
		}
	}

	private ArrayList<Integer> calculateRoles(Project project, String param){
		//Position within the list
		//Position 0 : Analyst ANA role
		//Position 1 : Programmer PRG role
		//Position 2 : Designer DES role
		//Position 3 : Tester TST role

		ArrayList<Integer> roleList = new ArrayList<Integer>();
		Resource resource;
		int ANA=0,DES=0,PRG=0,TST=0; 
		boolean fini = false;

		//Liste des project qui sont assigner presentement (dyan)	
		while (!fini) {
			resource =  param.equals("current") ? project.getResourcesAssigned().getNextResource() 
					: CommonData.theListOfResources.getListOfResources().getNextResource() ;

			if (resource == null) {
				fini = true;
			} else {
				if(param.equals("current")){
					String role=resource.getRole(); 

					if (role.equals("ANA")) 
						ANA++; 
					else if (role.equals("PRG")) 
						PRG++; 
					else if (role.equals("DES")) 
						DES++; 
					else if (role.equals("TST")) 
						TST++;


				}else{
					boolean pIsPresent = resource.getPreviouslyAssignedProjectList().findProject(project);
					if (pIsPresent) {
						String role=resource.getRole(); 

						if (role.equals("ANA")) 
							ANA++; 
						else if (role.equals("PRG")) 
							PRG++; 
						else if (role.equals("DES")) 
							DES++; 
						else if (role.equals("TST")) 
							TST++;
					}
				}
			} // if

		} // while

		roleList.add(0, ANA);
		roleList.add(1, PRG);
		roleList.add(2, DES);
		roleList.add(3, TST);


		return roleList;
	}	

}
